﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using XamlDom.Common;

namespace XamlDom.Runtime
{
    public class PropertiesRuntime
    {
        /// <summary>
        /// 获取Orientation方向
        /// </summary>
        /// <param name="orientationStr"></param>
        /// <returns></returns>
        public static Orientation GetOrientation(string orientationStr)
        {
            if (orientationStr.Equals("Horizontal"))
            {
                return Orientation.Horizontal;
            }
            else if (orientationStr.Equals("Vertical"))
            {
                return Orientation.Vertical;
            }
            else
            {
                return Orientation.Vertical;
            }
        }

        /// <summary>
        /// 获取Margin或者Padding
        /// </summary>
        /// <param name="marginOrPaddingStr"></param>
        /// <returns></returns>
        public static Thickness GetMarginOrPadding(string marginOrPaddingStr)
        {
            string[] splitStrArray = marginOrPaddingStr.Split(',');

            if (splitStrArray.Length == 1)
            {
                return new Thickness(GetDoubleValue(splitStrArray[0]), GetDoubleValue(splitStrArray[0]), GetDoubleValue(splitStrArray[0]), GetDoubleValue(splitStrArray[0]));
            }
            else if (splitStrArray.Length == 2)
            {
                return new Thickness(GetDoubleValue(splitStrArray[0]), GetDoubleValue(splitStrArray[1]), GetDoubleValue(splitStrArray[0]), GetDoubleValue(splitStrArray[1]));
            }
            else if (splitStrArray.Length == 4)
            {
                return new Thickness(GetDoubleValue(splitStrArray[0]), GetDoubleValue(splitStrArray[1]), GetDoubleValue(splitStrArray[2]), GetDoubleValue(splitStrArray[3]));
            }
            else
            {
                throw new ArgumentException("marginOrPaddingStr argument is invalid !");
            }
        }

        /// <summary>
        /// 根据字符串获取浮点数值
        /// 比方说获取宽度或者高度或者获取边距值
        /// </summary>
        /// <param name="valueStr"></param>
        /// <returns></returns>
        public static double GetDoubleValue(string valueStr)
        {
            try
            {
                return Convert.ToDouble(valueStr);
            }
            catch
            {
                throw new ArgumentException("Value string is invalid .");
            }
        }
    }
}
